package com.hook.analyse.util;

import java.io.*;
import java.util.*;
import com.alibaba.fastjson.*;

public class HDb
 {

    private String path;

    private HDb() {
    }

    private static HDb hDb;

    public static HDb getInstance(String path) {
        if (hDb == null) {
            hDb = new HDb();
            hDb.path = path;
        }
        return hDb;
    }

    public void write(List objects) {
        try {
            File parentFile = new File(path).getParentFile();
            if (!parentFile.isDirectory()) {
                parentFile.mkdirs();
            }
            FileWriter writer = new FileWriter(path);
            for (Object object : objects) {
                writer.write(JSON.toJSONString(object) + "\n");
            }
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
	public List read(Class clazz) {
		List data = new ArrayList<>();
		try {
			BufferedReader in = new BufferedReader(new FileReader(path));
			String line = in.readLine();
			while (line != null) {
				JSONObject jsonObject = JSON.parseObject(line);
				data.add(JSON.toJavaObject(jsonObject,clazz));
				line = in.readLine();
			}
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return data;
	}
	
	}
